/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.software;

import com.mapping.Software;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author PARDmx
 */
@Namespace(value="/")
@Action(value="buscarSoftwareP", results={@Result(name="success",location="/GestionSoftware/gestionSoftware.jsp")})
public class BuscarSoftwareAction extends ActionSupport {
    private String buscar;
    private List<Software> softwares;

    @Override
    public String execute() throws Exception {
        String consulta = "select IDSOFT,SOFT,DESCRIPCION from software where match (idSoft,soft) against (?1 IN BOOLEAN MODE)"; 
        System.out.println(consulta + " busca = "+ buscar);
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        if(buscar== null || buscar.equals("")){
             softwares = (List<Software>) em.createNamedQuery("Software.findAll").getResultList();
        }else{
            Query query =  em.createNativeQuery(consulta,Software.class);
            query.setParameter(1,buscar+"*");          
            softwares = query.getResultList();
        }
        tx.commit();
        em.close();        
        return SUCCESS;            
    }
    
    
}
